package com.gln.bloger.action;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.gln.bloger.constant.Constant;
import com.gln.bloger.pojos.Comment;
import com.gln.bloger.pojos.Entry;
import com.gln.bloger.pojos.Website;
import com.gln.bloger.util.PagingSupport;

/**
 * entry for user
 * Created on 20080115
 * @author renxj
 * modify entries/articles 
 */
public class EntryAction extends GuestBaseAction {
	
	/**
	 * show entry 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward show(ActionMapping mapping, ActionForm form,
		   HttpServletRequest request, HttpServletResponse response) 
	       throws Exception{
		String id = (String) request.getParameter("id");
		Entry current = getEntryService().getEntry(id);
		Entry previous = getEntryService().getPreviousEntry(getAuthWebsite(request), id);
		Entry next = getEntryService().getNextEntry(getAuthWebsite(request), id);
		List<Entry> relatedEntries = getEntryService().getRelatedEntries(getAuthWebsite(request), 
				                                         current, Constant.RELATED_ENTRY_SIZE);
		/**
		 * add hit count
		 * note here, the session which hold current entry has been closed,
		 * so we must save it manually  
		 */
		current.setHits(current.getHits()+1);
		getEntryService().saveOrUpdateEntry(current);
		List<Comment> comments = getCommentService().getComments(id);
		    request.setAttribute("comments", comments);
			request.setAttribute("previous", previous);
			request.setAttribute("next", next);
			request.setAttribute("entry", current);
			request.setAttribute("relatedEntries", relatedEntries);
		return mapping.findForward("showEntry");
	}
	
	@SuppressWarnings("unchecked")
	public ActionForward list(ActionMapping mapping, ActionForm form,
			   HttpServletRequest request, HttpServletResponse response) throws Exception{
		Website website = getAuthWebsite(request);	   
		String p = (String)request.getParameter("p");
			   PagingSupport<Entry>
				   entries = getEntryService().getEntries(website);

			   if(null != p){
				   entries.setCurrentPage(Integer.parseInt(p));
			   }
			   request.setAttribute("entries", entries);
			   return mapping.findForward("guestIndex");
	}
	
	@SuppressWarnings("unchecked")
	public ActionForward archive(ActionMapping mapping, ActionForm form,
			   HttpServletRequest request, HttpServletResponse response) throws Exception{
		Website website = getAuthWebsite(request);	
		String  monthStr = request.getParameter("month");
		String p = (String)request.getParameter("p");
		
		SimpleDateFormat sdf = new SimpleDateFormat(Constant.DATE_FORMAT_yyyyMM);
		Date month = sdf.parse(monthStr);
			   PagingSupport<Entry>
				   entries = getEntryService().getEntriesByMonth(website, month);
			   if(null != p){
				   entries.setCurrentPage(Integer.parseInt(p));
			   }
			   request.setAttribute("month", monthStr);
			   request.setAttribute("entries", entries);
			   return mapping.findForward("archivePage");
	}
}
